package cn.lzy.taskmanagement.AsyncHasResult;

import cn.lzy.taskmanagement.util.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * @Author: WeiKaiYe
 * @CreateTime: 2023-10-21  18:04
 */

@RestController
public class ResultAsyncController {
    @Autowired
    private ResultAsyncService resultAsyncService;

    //  http://localhost:8080/statistics

    @GetMapping("/statistics")
    public String statistics() throws Exception {  //同步+异步

        String data1 = DataUtils.getData(DataUtils.data1);
        System.out.println(" statistics 主流程-开始执行时间===" + data1);
        System.out.println(" ----------------------------------- ");

        Long startTime1 = System.currentTimeMillis(); // 获取当前时间
        Future<Integer> futureA = resultAsyncService.processA();   // 等待4S
        Future<Integer> futureB = resultAsyncService.processB();

        int total2 = futureA.get() + futureB.get();    // futureA.get() = 123456     futureB.get() = 654321
        System.out.println(" ----------------------------------- ");
        System.out.println(" statistics 主流程-计算值的总和:" + total2 + "===开始时间===" + DataUtils.getData(DataUtils.data1));

        Long end = System.currentTimeMillis();
        System.out.println(" statistics 主流程-结束时间 statistics==" + DataUtils.getData(DataUtils.data1));

        System.out.println(" statistics 主流程-总计耗时:" + (end - startTime1));
        return "success";
    }
}
